package com.sun.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.enums.HttpEnum;
import com.sun.mapper.SuppliersMapper;
import com.sun.pojo.entity.Suppliers;
import com.sun.pojo.vo.R;
import com.sun.pojo.vo.SuppliersListVO;
import com.sun.service.SuppliersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * (Suppliers)表服务实现类
 *
 * @author makejava
 * @since 2024-10-22 12:56:37
 */
@Service
public class SuppliersServiceImpl implements SuppliersService {
    @Autowired
    SuppliersMapper suppliersMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param supplierId 主键
     * @return 实例对象
     */
    @Override
    public Suppliers queryById(Integer supplierId) {
        return suppliersMapper.queryById(supplierId);
    }

    /**
     * 分页查询
     *
     * @param suppliers 筛选条件
     * @param pageNum      分页对象
     * @return 查询结果
     */
    @Override
    public PageInfo<Suppliers> queryByPage(Suppliers suppliers, Integer pageNum) {
        PageHelper.startPage(pageNum,5);
        List<Suppliers> list = suppliersMapper.queryAllByLimit(suppliers,pageNum);
        PageInfo<Suppliers> pageInfo =  new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param suppliers 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(Suppliers suppliers) {
       return suppliersMapper.insert(suppliers);
    }

    /**
     * 修改数据
     *
     * @param suppliers 实例对象
     * @return 实例对象
     */
    @Override
    public int update(Suppliers suppliers) {
        return suppliersMapper.update(suppliers);
    }

    /**
     * 通过主键删除数据
     *
     * @param supplierId 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(Integer supplierId) {
        return suppliersMapper.deleteById(supplierId);
    }

    @Override
    public List<SuppliersListVO> queryAll() {
        return suppliersMapper.queryAll();
    }

    @Override
    public R login(String email, String password) {
        Suppliers suppliers = suppliersMapper.loginByEmail(email);
        if (suppliers.getPassword().equals(password)){
            return R.success(suppliers);
        }
        return R.fail(HttpEnum.WARN.getCode(),"登录失败");
    }
}
